using System.Text.RegularExpressions;
using DZJIFC.Model;

namespace DZJIFC.Infrastructure.Import;






// Very lightweight line-based IFC text reader to bootstrap the workflow without native engine.
// It recognizes a few common element types and names by heuristic only.
public class MockIfcImporter : IIfcImporter
{
    private static readonly Regex _entity = new Regex(@"IFC(\w+)", RegexOptions.IgnoreCase|RegexOptions.Compiled);
    private static readonly Regex _name = new Regex(@"'([^']*)'", RegexOptions.Compiled);

    public IFCModel Import(string path)
    {
        var model = new IFCModel { /*SourcePath = path, Schema = "IFC2X3"*/ };
        int auto = 0;
        //foreach (var raw in System.IO.File.ReadLines(path))
        //{
        //    var line = raw.Trim();
        //    if (!line.StartsWith("#")) continue;
        //    var m = _entity.Match(line);
        //    if (!m.Success) continue;

        //    var type = "IFC" + m.Groups[1].Value;
        //    if (!type.StartsWith("IFCIFC")) type = type; // keep

        //    // Only take some MEP types into account to keep it small
        //    if (!(type.Contains("IFCFLOW") || type.Contains("IFCDISTRIBUTIONELEMENT") || type.Contains("IFCSPACE")))
        //        continue;

        //    var nameMatch = _name.Match(line);
        //    var name = nameMatch.Success ? nameMatch.Groups[1].Value : type + "_" + (++auto);

        //    var e = new IfcElement {
        //        Guid = System.Guid.NewGuid().ToString("N"),
        //        IfcType = type.ToUpperInvariant(),
        //        Name = name,
        //        Level = "", System = ""
        //    };
        //    e.PropertySets.Add(new IfcPropertySet{ Name = "Pset_Basic", Properties = new(){ new IfcProperty{ Name="Name", Value=name }}});
        //    model.Elements.Add(e);
        //}
        //if (model.Elements.Count == 0)
        //{
        //    // Fallback: add one dummy
        //    model.Elements.Add(new IfcElement {
        //        Guid = System.Guid.NewGuid().ToString("N"),
        //        IfcType = "IFCFLOWSEGMENT",
        //        Name = "Dummy",
        //        Level = "L1",
        //        System = "MEP",
        //        PropertySets = new(){ new IfcPropertySet{ Name="Pset_Basic", Properties = new(){ new IfcProperty{ Name="Name", Value="Dummy" } } } }
        //    });
        //}
        return model;
    }
}